import java.util.ArrayList;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-02-24
 * Time: 15:04
 */

public class TestDemo1 {
    //有效的括号
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();

        for(int i=0;i<s.length();i++){
            char ch = s.charAt(i);
            if(ch=='('||ch=='{'||ch=='['){
                stack.push(ch);
            }
            if(stack.peek()=='('||stack.peek()=='{'||stack.peek()=='['){
                stack.pop();
            }
        }
        return stack.empty();
    }

    public static void main(String[] args) {
        String str = "()";
        System.out.println(isValid(str));

    }
    public static void main3(String[] args) {
        MyLinkStack myLinkStack = new MyLinkStack();
        myLinkStack.push(1);
        myLinkStack.push(2);
        myLinkStack.push(3);
        myLinkStack.push(4);
        myLinkStack.push(5);
        System.out.println(myLinkStack.isEmpty());

        System.out.println(myLinkStack.pop());
        System.out.println(myLinkStack.pop());
        System.out.println(myLinkStack.pop());
        System.out.println(myLinkStack.pop());
        System.out.println(myLinkStack.pop());
        //System.out.println(myLinkStack.peek());

        System.out.println(myLinkStack.isEmpty());

    }

    public static void main2(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        myStack.push(4);
        System.out.println(myStack.peek());
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.isEmpty());
    }
//逆波兰表达式求值(中缀表达式转为后缀表达式）
    public static int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(int i=0;i<tokens.length;i++){
            String str = tokens[i];
            if(!isOperation(str)){
                stack.push(Integer.parseInt(str));
            }else{
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(str){
                    case"+": stack.push(num1+num2);break;
                    case"-": stack.push(num1-num2);break;
                    case"*": stack.push(num1*num2);break;
                    case"/": stack.push(num1/num2);break;
                }
            }
        }
        return stack.pop();
    }

    public static boolean isOperation(String str){
        if(str.equals("+")||str.equals("-")||str.equals("*")||str.equals("/")){
            return true;
        }
        return false;
    }
    public static void main1(String[] args) {
        String[] tokens = {"4","13","5","/","+"};
        int ret = evalRPN(tokens);
        System.out.println(ret);
    }

    // 栈的压入、弹出序列
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i=0;i<pushA.length;i++){
            stack.push(pushA[i]);
            while(j<popA.length && !stack.empty() && stack.peek() == popA[j]){
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }
}
